#pragma once
#include <iostream>
#include <string>
#include <sys/time.h>

// log level
enum
{
    INFO,
    DEBUG,
    WARNING,
    ERROR,
    FATAL
};

std::string GetTimeStamp();
std::string GetFileName(const std::string&);

// LOG() << "xxxxxx";
inline std::ostream& Log(const std::string& level, const std::string& file_name, int line)
{
    std::string message = "[" + level + "]";      // 添加日志等级

    message += "[" + GetTimeStamp() + "]";        // 获取日志时间
    message += "[" + GetFileName(file_name)  + "]";             // 获取报错文件
    message += "[" + std::to_string(line) + "]";  // 获取报错行号

    std::cout << message + " "; // 存入缓冲区，不刷新待填充报错信息
    return std::cout;
}

#define LOG(level) Log(#level, __FILE__, __LINE__)

std::string GetTimeStamp()
{
    struct timeval time_val;
    gettimeofday(&time_val, nullptr);

    return std::to_string(time_val.tv_sec);
}

std::string GetFileName(const std::string& file_name)
{
    int pos = file_name.find_last_of('/');
    if (pos == std::string::npos)
        return "error file name";
    return file_name.substr(pos + 1);
}